//
//  PersonPubViewController.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/8/24.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "PersonPubViewController.h"
#import "WofabudeTableViewCell.h"
#import "PostJinengViewController.h"
#import "PostHuoDongViewController.h"
#import "BaomingLieBiaoViewController.h"
#import "HomeDetailViewController.h"
#import "JobDetailViewController.h"
#import "ChatuViewController.h"
@interface PersonPubViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * contentTableView;
    UIButton * button1;
    UIButton * button2;
    UIButton * button3;
    UIView * indicator;
    NSArray * dataArr;
}

@end

@implementation PersonPubViewController

-(void)requestData
{
    if (button1.selected)
    {
        [NetRequestCenter lessonlistwithPage:@"1" uid:self.contentId Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
    else if (button2.selected)
    {
        [NetRequestCenter demandlistpage:@"1" uid:self.contentId Result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
    else
    {
        [NetRequestCenter activitylistwithuid:self.contentId page:@"1" result:^(BOOL success, NSDictionary *resultDic) {
            if (success) {
                dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
                [contentTableView reloadData];
            }
        }];
    }
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self requestData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.title = @"";
    
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth/3, 50)];
    [button1 addTarget:self action:@selector(button1Action) forControlEvents:UIControlEventTouchUpInside];
    button1.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button1 setTitle:@"技能" forState:UIControlStateNormal];
    [self.view addSubview:button1];
    
    button2 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3, rectStatus.size.height + rectNav.size.height, ScreenWidth/3, 50)];
    [button2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
    button2.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button2 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button2 setTitle:@"需求" forState:UIControlStateNormal];
    [self.view addSubview:button2];
    
    button3 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3 * 2, rectStatus.size.height + rectNav.size.height, ScreenWidth/3, 50)];
    [button3 addTarget:self action:@selector(button3Action) forControlEvents:UIControlEventTouchUpInside];
    button3.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button3 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button3 setTitle:@"活动" forState:UIControlStateNormal];
    [self.view addSubview:button3];
    
    indicator = [[UIView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height + 48, 60, 2)];
    indicator.backgroundColor = themeColor;
    [self.view addSubview:indicator];
    
    indicator.center = CGPointMake(ScreenWidth/6, indicator.center.y);
    button1.selected = YES;
    
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height + 50, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height - 50) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
    
    if (self.preselect == 1)
    {
        [self button1Action];
    }
    else if (self.preselect == 2)
    {
        [self button2Action];
    }
    else
    {
        [self button3Action];
    }
}

-(void)button1Action
{
    button1.selected = YES;
    button2.selected = NO;
    button3.selected = NO;
    [self requestData];
    indicator.center = CGPointMake(ScreenWidth/6, indicator.center.y);
}

-(void)button2Action
{
    button1.selected = NO;
    button2.selected = YES;
    button3.selected = NO;
    [self requestData];
    indicator.center = CGPointMake(ScreenWidth/6 * 3, indicator.center.y);
}

-(void)button3Action
{
    button1.selected = NO;
    button2.selected = NO;
    button3.selected = YES;
    [self requestData];
    indicator.center = CGPointMake(ScreenWidth/6 * 5, indicator.center.y);
}

#pragma mark - tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [WofabudeTableViewCell height];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifer = @"identifer";
    WofabudeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[WofabudeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    if (button1.selected)
    {
        [cell loadData:[dataArr objectAtIndex:indexPath.row] int:1 other:0];
    }
    else if (button2.selected)
    {
        [cell loadData:[dataArr objectAtIndex:indexPath.row] int:2 other:0];
    }
    else
    {
        [cell loadData:[dataArr objectAtIndex:indexPath.row] int:3 other:0];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.other != 0)
    {
        if (button1.selected || button2.selected)
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                                     message:nil
                                                                              preferredStyle:UIAlertControllerStyleActionSheet];

            UIAlertAction * item1 = [UIAlertAction actionWithTitle:@"报名列表" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                BaomingLieBiaoViewController * baoming = [[BaomingLieBiaoViewController alloc]init];
                baoming.contentId = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"TaskID"];
                [self.navigationController pushViewController:baoming animated:YES];
            }];
            UIAlertAction * item2 = [UIAlertAction actionWithTitle:@"下架" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [SVProgressHUD show];
                [NetRequestCenter demandlessonofwithtaskid:[[dataArr objectAtIndex:indexPath.row]objectForKey:@"TaskID"] result:^(BOOL success, NSDictionary *resultDic) {
                    if (success)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"已下架"];
                        [self requestData];
                    }
                    else
                    {
                        [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
                    }
                }];
            }];
            UIAlertAction * item3 = [UIAlertAction actionWithTitle:@"查看详情" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                JobDetailViewController * job = [[JobDetailViewController alloc]init];
                if (button1.selected)
                {
                    job.demand = NO;
                }
                else
                {
                    job.demand = YES;
                }
                job.contentid = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"TaskID"];
                [self.navigationController pushViewController:job animated:YES];
            }];
            UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];

            [alertController addAction:item1];
            [alertController addAction:item2];
            [alertController addAction:item3];
            [alertController addAction:cancelAction];

            [self presentViewController:alertController animated:YES completion:nil];
        }
        else
        {
            NSDictionary * data = [dataArr objectAtIndex:indexPath.row];
            NSString * text;
            if ([[data objectForKey:@"TaskOffShelfSee"]isEqualToNumber:@1])
            {
                text = @"过期不可见";
            }
            else
            {
                text = @"过期可见";
            }
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                                     message:nil
                                                                              preferredStyle:UIAlertControllerStyleActionSheet];

            UIAlertAction * item1 = [UIAlertAction actionWithTitle:@"报名列表" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                BaomingLieBiaoViewController * baoming = [[BaomingLieBiaoViewController alloc]init];
                baoming.contentId = [data objectForKey:@"TaskID"];
                [self.navigationController pushViewController:baoming animated:YES];
            }];
            UIAlertAction * item2 = [UIAlertAction actionWithTitle:text style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [SVProgressHUD show];
                [NetRequestCenter activitydenyviewwithtaskid:[data objectForKey:@"TaskID"] result:^(BOOL success, NSDictionary *resultDic) {
                    if (success) {
                        [SVProgressHUD showSuccessWithStatus:@"成功"];
                    }
                    else
                    {
                        [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
                    }
                }];
            }];
            UIAlertAction * item3 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [SVProgressHUD show];
                [NetRequestCenter activitydeletewithtaskid:[data objectForKey:@"TaskID"] result:^(BOOL success, NSDictionary *resultDic) {
                    if (success)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"已删除"];
                        [self requestData];
                    }
                    else
                    {
                        [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
                    }
                }];
            }];
            UIAlertAction * item4 = [UIAlertAction actionWithTitle:@"插图" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                ChatuViewController * chatu = [[ChatuViewController alloc]init];
                chatu.contentId = [data objectForKey:@"TaskID"];
                [self.navigationController pushViewController:chatu animated:YES];
            }];
            UIAlertAction * item5 = [UIAlertAction actionWithTitle:@"查看详情" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                HomeDetailViewController * homeD = [[HomeDetailViewController alloc]init];
                homeD.contentid = [data objectForKey:@"TaskID"];
                [self.navigationController pushViewController:homeD animated:YES];
            }];
            UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];

            [alertController addAction:item1];
            [alertController addAction:item2];
            [alertController addAction:item3];
            [alertController addAction:item4];
            [alertController addAction:item5];

            [alertController addAction:cancelAction];

            [self presentViewController:alertController animated:YES completion:nil];
        }

    } else {
        if (button1.selected || button2.selected) {
            JobDetailViewController * job = [[JobDetailViewController alloc]init];
            if (button1.selected)
            {
                job.demand = NO;
            }
            else
            {
                job.demand = YES;
            }
            job.contentid = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"TaskID"];
            [self.navigationController pushViewController:job animated:YES];
        } else {
            NSDictionary * data = [dataArr objectAtIndex:indexPath.row];
            HomeDetailViewController * homeD = [[HomeDetailViewController alloc]init];
            homeD.contentid = [data objectForKey:@"TaskID"];
            [self.navigationController pushViewController:homeD animated:YES];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
